﻿// ***********************************************************************
// Assembly         : MVC4Portal
// Author           : Lolo
// Created          : 10-25-2013
//
// Last Modified By : Lolo
// Last Modified On : 10-25-2013
// ***********************************************************************
// <copyright file="AdminSettingsController.cs" company="Innovacall">
//     Copyright (c) Innovacall. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using EmitMapper;
using InnovaApp.Layers.CommonLayer;
using InnovaApp.Layers.DataLayer;
using InnovaApp.Layers.DomainLayer;
using InnovaApp.Layers.ResourcesLayer.Properties;
using InnovaApp.Portals.MVC4Portal.Models.JSonModels;
using InnovaApp.Portals.MVC4Portal.Models.MetaModels.ApplicationSettings;
using InnovaApp.Portals.MVC4Portal.Models.ViewModels;
using INNOVACALL.Framework.Common.Models;
using INNOVACALL.Framework.Common.MVCHelpers;
using INNOVACALL.Framework.Common.InnovacallExceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.Transactions;
using INNOVACALL.Framework.Common;
using InnovaApp.Layers.CommonLayer.Models.ViewModels.LayoutViewModels;

namespace InnovaApp.Portals.MVC4Portal.Controllers.ViewControllers
{
    /// <summary>
    /// Class AdminSettingsController.
    /// </summary>
    [Authorize(Roles = "Admin")]
    public class AdminSettingsController : _ViewControllers_Base<AdminSettings_ViewModel>
    {
        /// <summary>
        /// Prépare Settings dialog
        /// </summary>
        /// <returns>ActionResult.</returns>
        [Authorize(Roles = "Admin"), HttpPost, CacheManager(enCacheMode.NoCache)]
        public JsonResult AdminSettingsFeed(string language, Simple_JSonModel ajaxModel)
        {
            JsonResult toReturn = new JsonResult { JsonRequestBehavior = JsonRequestBehavior.AllowGet };
            AdminSettings_ViewModel toGenerateViewModel = new AdminSettings_ViewModel();
            string eMethod = eMethodBase + "AdminSettingsFeed[POST]";
            object eParameters = new { language = language };
            bool success = false;
            string htmlContent = string.Empty;
            string scriptToExecute = string.Empty;
            string messageTitle = string.Empty;
            string message = string.Empty;
            string dataRangeMin = string.Empty;
            string dataRangeMax = string.Empty;

            INNOVACALL.Framework.Common.MVCHelpers.Localization.CommonLocalization.SetCurrentThreadLanguage(language);
            try
            {
                using (DataRepositories _dataContext = new DataRepositories())
                {
                    InnoApplication dbInnoApplication = _dataContext.InnoApplication_Rep.Query(x => x.Name == Membership.ApplicationName).FirstOrDefault();
                    if (dbInnoApplication == null)
                    {
                        throw new INNOVACALL.Framework.Common.InnovacallExceptions.DataIntegrityException(eMethod, eParameters, "CRITICAL ! Application not found");
                    }

                    InnoAppSettings dbInnoAppSettings = _dataContext.InnoAppSettings_Rep.GetById(1);
                    if (dbInnoAppSettings == null)
                    {
                        toGenerateViewModel.Settings.AccountActivationFromEmailAddress = GetAccountActivationFromMailAddress();
                        toGenerateViewModel.Settings.SiteLayout = PortalEnvironment.SiteLayout.ToString();
                        toGenerateViewModel.Settings.FK_InnoApplication = dbInnoApplication.Id;
                        toGenerateViewModel.Settings.Id = 1;
                        toGenerateViewModel.Settings.JQueryCornersRounded = true;
                        toGenerateViewModel.Settings.JQueryUITheme = PortalEnvironment.JQueryUITheme;
                        toGenerateViewModel.Settings.KendoUITheme = PortalEnvironment.KendoUITheme;
                    }
                    else
                    {
                        toGenerateViewModel.Settings = ObjectMapperManager.DefaultInstance.GetMapper<InnoAppSettings, InnoAppSettings_MetaModel>().Map(dbInnoAppSettings);
                    }
                    IEnumerable<InnoAppDynamicSetting> dbSecondarySettings = _dataContext.InnoAppDynamicSetting_Rep.GetAll();
                    foreach (InnoAppDynamicSetting dbSecondarySetting in dbSecondarySettings)
                    {
                        InnoAppDynamicSetting_MetaModel secondarySettingToAdd = ObjectMapperManager.DefaultInstance.GetMapper<InnoAppDynamicSetting, InnoAppDynamicSetting_MetaModel>().Map(dbSecondarySetting);
                        secondarySettingToAdd.ValueHtmlAttributes.Add("class", "k-input");
                        if (secondarySettingToAdd.Required)
                        {
                            secondarySettingToAdd.ValueHtmlAttributes.Add("data-val-required", "*");
                        }
                        secondarySettingToAdd.enExpectedType = EnumTools.EnumTypeTo<enDynamicSettingsExpectedTypes>(dbSecondarySetting.ExpectedType);
                        secondarySettingToAdd.ExpectedType = INNOVACALL.Framework.Common.MVCHelpers.Localization.CommonLocalization.LookupResource(typeof(Resources), StringEnum.GetStringValue(secondarySettingToAdd.enExpectedType));
                        switch (secondarySettingToAdd.enExpectedType)
                        {
                            #region Case Bool
                            case enDynamicSettingsExpectedTypes.typeBool:
                                //Nothing to do
                                break;
                            #endregion
                            #region Case Int
                            case enDynamicSettingsExpectedTypes.typeInt:
                                if (string.IsNullOrEmpty(secondarySettingToAdd.Range_From))
                                {
                                    dataRangeMin = int.MinValue.ToString();
                                }
                                else
                                {
                                    dataRangeMin = int.Parse(secondarySettingToAdd.Range_From).ToString();
                                }
                                if (string.IsNullOrEmpty(secondarySettingToAdd.Range_To))
                                {
                                    dataRangeMax = int.MaxValue.ToString();
                                }
                                else
                                {
                                    dataRangeMax = int.Parse(secondarySettingToAdd.Range_To).ToString();
                                }
                                secondarySettingToAdd.ValueHtmlAttributes.Add("data-val-range-min", dataRangeMin);
                                secondarySettingToAdd.ValueHtmlAttributes.Add("data-val-range-max", dataRangeMax);
                                secondarySettingToAdd.ValueHtmlAttributes.Add("data-val-range", string.Format(Resources.View_AdminSetting_val_DataRange, dataRangeMin, dataRangeMax));
                                if (int.Parse(dataRangeMin) < 0)
                                {
                                    secondarySettingToAdd.ValueHtmlAttributes.Add("data-val-regex-pattern", @"^[-+]?\d+$");
                                    secondarySettingToAdd.ValueHtmlAttributes.Add("data-val-regex", Resources.View_AdminSetting_val_SignedIntegerFormat);
                                }
                                else
                                {
                                    secondarySettingToAdd.ValueHtmlAttributes.Add("data-val-regex-pattern", @"\d+$");
                                    secondarySettingToAdd.ValueHtmlAttributes.Add("data-val-regex", Resources.View_AdminSetting_val_PositiveIntegerFormat);
                                }
                                break;
                            #endregion
                            #region others
                            case enDynamicSettingsExpectedTypes.typeDouble:
                                if (string.IsNullOrEmpty(secondarySettingToAdd.Range_From))
                                {
                                    dataRangeMin = double.MinValue.ToString();
                                }
                                else
                                {
                                    dataRangeMin = int.Parse(secondarySettingToAdd.Range_From).ToString();
                                }
                                if (string.IsNullOrEmpty(secondarySettingToAdd.Range_To))
                                {
                                    dataRangeMax = double.MaxValue.ToString();
                                }
                                else
                                {
                                    dataRangeMax = int.Parse(secondarySettingToAdd.Range_To).ToString();
                                }
                                secondarySettingToAdd.ValueHtmlAttributes.Add("data-val-range", string.Format(Resources.View_AdminSetting_val_DataRange, dataRangeMin, dataRangeMax));
                                break;
                            case enDynamicSettingsExpectedTypes.typePercent:
                                if (string.IsNullOrEmpty(secondarySettingToAdd.Range_From))
                                {
                                    dataRangeMin = "0";
                                }
                                else
                                {
                                    dataRangeMin = int.Parse(secondarySettingToAdd.Range_From).ToString();
                                }
                                if (string.IsNullOrEmpty(secondarySettingToAdd.Range_To))
                                {
                                    dataRangeMax = "100";
                                }
                                else
                                {
                                    dataRangeMax = int.Parse(secondarySettingToAdd.Range_To).ToString();
                                }
                                secondarySettingToAdd.ValueHtmlAttributes.Add("data-val-range", string.Format(Resources.View_AdminSetting_val_DataRange, dataRangeMin, dataRangeMax));
                                secondarySettingToAdd.ValueHtmlAttributes.Add("data-val-regex-pattern", @"^(100\.00|100\.0|100)|([0-9]{1,2}){0,1}(\.[0-9]{1,2}){0,1}$");
                                secondarySettingToAdd.ValueHtmlAttributes.Add("data-val-regex", Resources.View_AdminSetting_val_PercentFormat);
                                break;
                            case enDynamicSettingsExpectedTypes.typeDate:
                                secondarySettingToAdd.ValueHtmlAttributes.Add("data-val-regex-pattern", @"^(((((0[1-9])|(1\d)|(2[0-8]))\/((0[1-9])|(1[0-2])))|((31\/((0[13578])|(1[02])))|((29|30)\/((0[1,3-9])|(1[0-2])))))\/((20[0-9][0-9])|(19[0-9][0-9])))|((29\/02\/(19|20)(([02468][048])|([13579][26]))))$");
                                secondarySettingToAdd.ValueHtmlAttributes.Add("data-val-regex", Resources.View_AdminSetting_val_Date);
                                break;
                            case enDynamicSettingsExpectedTypes.typeTime:
                                secondarySettingToAdd.ValueHtmlAttributes.Add("data-val-regex-pattern", @"^(?:(?:([01]?\d|2[0-3]):)?([0-5]?\d):)?([0-5]?\d)$");
                                secondarySettingToAdd.ValueHtmlAttributes.Add("data-val-regex", Resources.View_AdminSetting_val_Time);
                                break;
                            case enDynamicSettingsExpectedTypes.typeDateTime:
                                secondarySettingToAdd.ValueHtmlAttributes.Add("data-val-regex-pattern", @"^(?:(?:([01]?\d|2[0-3]):)?([0-5]?\d):)?([0-5]?\d)$");
                                secondarySettingToAdd.ValueHtmlAttributes.Add("data-val-regex", Resources.View_AdminSetting_val_DateTime);
                                break;
                            case enDynamicSettingsExpectedTypes.typeString:
                                //Nothing to do
                                break;
                            #endregion

                        }
                        secondarySettingToAdd.Name = INNOVACALL.Framework.Common.MVCHelpers.Localization.CommonLocalization.LookupResource(typeof(Resources), "View_AdminSettings_" + dbSecondarySetting.SiteLayout + "_" + dbSecondarySetting.Name);
                        secondarySettingToAdd.ExpectedTypes = GetExpectedTypes(dbSecondarySetting.ExpectedType);
                        toGenerateViewModel.SecondarySettings.Add(secondarySettingToAdd);
                    }
                    success = true;
                    htmlContent = base.RenderPartialViewToString("_AdminSettings", toGenerateViewModel);
                    scriptToExecute = string.Empty;
                    messageTitle = string.Empty;
                    message = string.Empty;

                    toReturn.Data = new INNOVACALLAjaxResponse(
                        success: true,
                        domElementToUpdate: ajaxModel.HtmlElementToUpdate,
                        htmlContent: htmlContent,
                        scriptToExecute: scriptToExecute,
                        messageTitle: messageTitle,
                        message: message,
                        additionalData: new { @modelToFeed = toGenerateViewModel }
                    );
                }

            }
            catch (Exception ex)
            {
                INNOVACALL.Framework.Common.InnovacallExceptions.CommonException iex = new INNOVACALL.Framework.Common.InnovacallExceptions.CommonException(eMethod, eParameters, ex.Message);
                iex.LogIt();
                toReturn.Data = new INNOVACALLAjaxResponse(
                    success: false,
                    domElementToUpdate: null,
                    htmlContent: null,
                    scriptToExecute: "$.fn.jqDialogAlert(a.messageTitle ,a.message,'');",
                    messageTitle: Resources.View_All_DialogErrorTitle,
                    message: ex.LayoutException(),
                    additionalData: null
                );
            }
            return toReturn;
        }

        private List<SelectListItem> GetExpectedTypes(string selectedValue)
        {
            List<SelectListItem> toReturn = new List<SelectListItem>();
            toReturn.Add(new SelectListItem() { Value = "", Text = "", Selected = string.IsNullOrEmpty(selectedValue) });
            toReturn.Add(new SelectListItem() { Value = "bool", Text = "Bool", Selected = selectedValue == "bool" });
            toReturn.Add(new SelectListItem() { Value = "int", Text = "Int", Selected = selectedValue == "int" });
            toReturn.Add(new SelectListItem() { Value = "double", Text = "Decimal", Selected = selectedValue == "int" });
            toReturn.Add(new SelectListItem() { Value = "string", Text = "String", Selected = selectedValue == "string" });
            toReturn.Add(new SelectListItem() { Value = "date", Text = "Date", Selected = selectedValue == "date" });
            toReturn.Add(new SelectListItem() { Value = "time", Text = "Time", Selected = selectedValue == "time" });
            toReturn.Add(new SelectListItem() { Value = "dateTime", Text = "Date time", Selected = selectedValue == "datetime" });
            toReturn.Add(new SelectListItem() { Value = "percent", Text = "Percent", Selected = selectedValue == "datetime" });
            return toReturn;
        }


        [Authorize(Roles = "Admin"), HttpGet]
        public ActionResult Sample(string language)
        {
            string eMethod = eMethodBase + ".Sample[GET]";
            bool roundedCorners = true;
            bool.TryParse(Request.QueryString["roundedcorners"], out roundedCorners);
            Sample_ViewModel model = new Sample_ViewModel()
            {
                RoundedCorners = roundedCorners,
                JQueryUITheme = "/Content/themes/" + Request.QueryString["jquerytheme"] + "/jquery-ui-1.9.2.custom.min.css",
                KendoUITheme = "/Content/ClientComponents/KendoUI/kendo." + Request.QueryString["kendouitheme"] + ".min.css"
            };
            return View(model);
        }

        /// <summary>
        /// Profiles the detail Telecom add edit.
        /// Uses standard MVC Ajax system result in MainAjaxSuccess
        /// </summary>
        /// <param name="language">The language.</param>
        /// <param name="model">The model.</param>
        /// <returns>JsonResult.</returns>
        [HttpPost, Authorize, CacheManager(enCacheMode.NoCache)]
        public JsonResult AdminSettingsSave(string language, AdminSettings_ViewModel model)
        {
            string eMethod = eMethodBase + "AdminSettingsSave[Post]";
            string scriptToExecute = string.Empty;
            object eParameters = new { language = language, model = model };
            bool createIt = false;
            string report = string.Empty;
            string dataRangeMin = string.Empty;
            string dataRangeMax = string.Empty;
            JsonResult toReturn = new JsonResult { JsonRequestBehavior = JsonRequestBehavior.AllowGet };
            INNOVACALL.Framework.Common.MVCHelpers.Localization.CommonLocalization.SetCurrentThreadLanguage(language);
            string lf = string.Empty;
            using (TransactionScope _ts = new TransactionScope())
            {
                try
                {
                    using (DataRepositories _dataContext = new DataRepositories())
                    {
                        InnoApplication dbInnoApplication = _dataContext.InnoApplication_Rep.Query(x => x.Name == Membership.ApplicationName).FirstOrDefault();
                        if (dbInnoApplication == null)
                        {
                            throw new INNOVACALL.Framework.Common.InnovacallExceptions.DataIntegrityException(eMethod, eParameters, "CRITICAL ! Application not found");
                        }

                        InnoAppSettings dbInnoAppSettings = _dataContext.InnoAppSettings_Rep.GetById(1);
                        if (dbInnoAppSettings == null)
                        {
                            dbInnoAppSettings = new InnoAppSettings();
                            dbInnoAppSettings.Id = 1;
                            createIt = true;
                        }
                        dbInnoAppSettings.AccountActivationFromEmailAddress = model.Settings.AccountActivationFromEmailAddress;
                        dbInnoAppSettings.SiteLayout = model.Settings.SiteLayout;
                        dbInnoAppSettings.FK_InnoApplication = dbInnoApplication.Id;
                        dbInnoAppSettings.JQueryCornersRounded = model.Settings.JQueryCornersRounded;
                        dbInnoAppSettings.JQueryUITheme = model.Settings.JQueryUITheme;
                        dbInnoAppSettings.KendoUITheme = model.Settings.KendoUITheme;
                        if (createIt)
                        {
                            _dataContext.InnoAppSettings_Rep.Add(dbInnoAppSettings);
                        }
                        _dataContext.Save();
                        scriptToExecute = "$.fn.AdminSettingsSaved()";
                        toReturn.Data = new INNOVACALLAjaxResponse(
                            success: true,
                            domElementToUpdate: model.DialogToClose,
                            htmlContent: null,
                            scriptToExecute: scriptToExecute,
                            messageTitle: null,
                            message: null,
                            additionalData: null
                        );
                        //Save secondaries parameters

                        foreach (var p in model.SecondarySettings)
                        {
                            bool parsed = true;
                            InnoAppDynamicSetting dbInnoAppDynamicSetting = _dataContext.InnoAppDynamicSetting_Rep.Query(x => x.Id == p.Id && x.FK_InnoApplication == dbInnoApplication.Id).FirstOrDefault();
                            if (dbInnoAppDynamicSetting == null)
                            {
                                throw new INNOVACALL.Framework.Common.InnovacallExceptions.DataIntegrityException(eMethod, eParameters, string.Format("The dynamic parameter {0} was missing.", p.Name));
                            }
                            switch (dbInnoAppDynamicSetting.ExpectedType)
                            {
                                case "bool":
                                    bool boolResult;
                                    parsed = bool.TryParse(p.Value, out boolResult);
                                    if (!parsed)
                                    {
                                        report += string.Format(Resources.View_AdminSettings_errMsg_FieldCannotBeConvertedTo, p.Name, p.Value, "bool");
                                        p.Value = dbInnoAppDynamicSetting.Value;
                                    }
                                    else
                                    {
                                        p.Value = boolResult.ToString();
                                    }

                                    break;
                                case "int":
                                    int intResult;
                                    parsed = int.TryParse(p.Value, out intResult);
                                    if (!parsed)
                                    {
                                        report += string.Format(Resources.View_AdminSettings_errMsg_FieldCannotBeConvertedTo, p.Name, p.Value, "int");
                                        p.Value = dbInnoAppDynamicSetting.Value;
                                    }
                                    else
                                    {
                                        p.Value = intResult.ToString();
                                    }
                                    break;
                                case "double":
                                    double doubleResult;
                                    parsed = double.TryParse(p.Value, out doubleResult);
                                    if (!parsed)
                                    {
                                        report += string.Format(Resources.View_AdminSettings_errMsg_FieldCannotBeConvertedTo, p.Name, p.Value, "double");
                                        p.Value = dbInnoAppDynamicSetting.Value;
                                    }
                                    else
                                    {
                                        p.Value = doubleResult.ToString();
                                    }
                                    break;
                                case "time":
                                    DateTime timeResult = new DateTime(2000, 1, 1);
                                    parsed = DateTime.TryParse(string.Format("01/01/2000 {0}", p.Value), out timeResult);
                                    if (!parsed)
                                    {
                                        report += string.Format(Resources.View_AdminSettings_errMsg_FieldCannotBeConvertedTo, p.Name, p.Value, "time");
                                        p.Value = dbInnoAppDynamicSetting.Value;
                                    }
                                    else
                                    {
                                        p.Value = timeResult.ToString();
                                    }
                                    break;
                                case "date":
                                    DateTime dateResult;
                                    parsed = DateTime.TryParse(string.Format("{0} 00:00:00", p.Value), out dateResult);
                                    if (!parsed)
                                    {
                                        report += string.Format(Resources.View_AdminSettings_errMsg_FieldCannotBeConvertedTo, p.Name, p.Value, "date");
                                        p.Value = dbInnoAppDynamicSetting.Value;
                                    }
                                    else
                                    {
                                        p.Value = dateResult.ToString();
                                    }
                                    break;
                                case "dateTime":
                                    DateTime dateTimeResult;
                                    parsed = DateTime.TryParse(p.Value, out dateTimeResult);
                                    if (!parsed)
                                    {
                                        report += string.Format(Resources.View_AdminSettings_errMsg_FieldCannotBeConvertedTo, p.Name, p.Value, "date");
                                        p.Value = dbInnoAppDynamicSetting.Value;
                                    }
                                    else
                                    {
                                        p.Value = dateTimeResult.ToString();
                                    }
                                    break;
                                default://strings
                                    p.Value = p.Value.Trim();
                                    break;
                            }
                            dbInnoAppDynamicSetting.Value = p.Value;
                            _dataContext.Save();
                        }
                        PortalEnvironment.Refresh(_dataContext);
                    }
                    if (!string.IsNullOrEmpty(report))
                    {
                        throw new INNOVACALL.Framework.Common.InnovacallExceptions.TypeParsingException(eMethod, eParameters, report);
                    }
                    _ts.Complete();
                }

                catch (Exception ex)
                {
                    _ts.Dispose();
                    INNOVACALL.Framework.Common.InnovacallExceptions.CommonException iex = new INNOVACALL.Framework.Common.InnovacallExceptions.CommonException(eMethod, eParameters, ex.Message);
                    iex.LogIt();
                    toReturn.Data = new INNOVACALLAjaxResponse(
                        success: false,
                        domElementToUpdate: null,
                        htmlContent: null,
                        scriptToExecute: "$.fn.jqDialogAlert(a.messageTitle ,a.message,'');",
                        messageTitle: Resources.View_All_DialogErrorTitle,
                        message: ex.LayoutException(),
                        additionalData: null
                    );
                }
            }
            return toReturn;
        }

        [HttpPost, Authorize, CacheManager(enCacheMode.NoCache)]
        public JsonResult AdminSettingsLayoutSave(string language, AdminSettingsLayout_JSonModel ajaxModel)
        {
            string eMethod = eMethodBase + "AdminSettingsLayoutSave[Post]";
            string scriptToExecute = string.Empty;
            object eParameters = new { language = language, ajaxModel = ajaxModel };
            bool createIt = false;
            JsonResult toReturn = new JsonResult { JsonRequestBehavior = JsonRequestBehavior.AllowGet };
            INNOVACALL.Framework.Common.MVCHelpers.Localization.CommonLocalization.SetCurrentThreadLanguage(language);
            using (TransactionScope _ts = new TransactionScope())
            {
                try
                {
                    using (DataRepositories _dataContext = new DataRepositories())
                    {
                        InnoApplication dbInnoApplication = _dataContext.InnoApplication_Rep.Query(x => x.Name == Membership.ApplicationName).FirstOrDefault();
                        if (dbInnoApplication == null)
                        {
                            throw new INNOVACALL.Framework.Common.InnovacallExceptions.DataIntegrityException(eMethod, eParameters, "CRITICAL ! Application not found");
                        }

                        InnoAppSettings dbInnoAppSettings = _dataContext.InnoAppSettings_Rep.GetById(1);
                        if (dbInnoAppSettings == null)
                        {
                            dbInnoAppSettings = new InnoAppSettings();
                            dbInnoAppSettings.Id = 1;
                            createIt = true;
                        }
                        dbInnoAppSettings.FK_InnoApplication = dbInnoApplication.Id;
                        dbInnoAppSettings.SiteLayout = dbInnoAppSettings.SiteLayout;
                        if (createIt)
                        {
                            _dataContext.InnoAppSettings_Rep.Add(dbInnoAppSettings);
                        }
                        _dataContext.Save();
                        List<InnoAppDynamicSetting> dbInnoAppDynamicSettings = _dataContext.InnoAppDynamicSetting_Rep.Query(x => x.FK_InnoApplication == dbInnoApplication.Id && x.SiteLayout == dbInnoAppSettings.SiteLayout).ToList();
                        foreach (DynamicSetting_JSonModel dynamicSetting in ajaxModel.DynamicSettings)
                        {
                            dbInnoAppDynamicSettings.Where(x => x.Name == dynamicSetting.Key).Single().Value = dynamicSetting.Value.ToString();
                        }
                        _dataContext.Save();

                        scriptToExecute = "$.fn.AdminSettingsSaved()";
                        toReturn.Data = new INNOVACALLAjaxResponse(
                            success: true,
                            domElementToUpdate: null,
                            htmlContent: null,
                            scriptToExecute: scriptToExecute,
                            messageTitle: null,
                            message: null,
                            additionalData: null
                        );
                    }
                    _ts.Complete();
                }
                catch (Exception ex)
                {
                    _ts.Dispose();
                    INNOVACALL.Framework.Common.InnovacallExceptions.CommonException iex = new INNOVACALL.Framework.Common.InnovacallExceptions.CommonException(eMethod, eParameters, ex.Message);
                    iex.LogIt();
                    toReturn.Data = new INNOVACALLAjaxResponse(
                        success: false,
                        domElementToUpdate: null,
                        htmlContent: null,
                        scriptToExecute: "$.fn.jqDialogAlert(a.messageTitle ,a.message,'');",
                        messageTitle: Resources.View_All_DialogErrorTitle,
                        message: ex.LayoutException(),
                        additionalData: null
                    );
                }
            }
            PortalEnvironment.Refresh();
            return toReturn;
        }


        /// <summary>
        /// Gets the account activation from mail address.
        /// </summary>
        /// <returns>System.String.</returns>
        private string GetAccountActivationFromMailAddress()
        {
            string toReturn = "myprettycms@innovacall.com";
            if (!string.IsNullOrEmpty(PortalEnvironment.AccountActivationFromMailAddress))
            {
                toReturn = PortalEnvironment.AccountActivationFromMailAddress;
            }
            return toReturn;
        }

    }
}
